// pages/personal/personal.js
import request from '../../utils/request'
//手指接触的起始坐标
let startY = 0
//手指移动后的坐标
let moveY = 0
//手指移动的距离
let moveDistance = 0


Page({

  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: 'translateY(0)',
    coverTransition: '',
    //用户数据
    userInfo: {},
    //用户的播放记录
    rencentPlayList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {



    //加载用户信息
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      //更新userInfo状态
      this.setData({
        userInfo:JSON.parse(userInfo)
      }),
      this.getUserRencentPlayList(this.data.userInfo.userId)
    }


  },
  //获取用户的播放记录
  async getUserRencentPlayList(userId) {
    const rencentPlayData = await request('/user/record', { uid: userId, type: 0 })
    let index = 0
    let rencentPlayList = rencentPlayData.allData.slice(0, 10).map(item => {
      item.id = index++;
      return item;
    })
    this.setData({
      rencentPlayList
    })
  },
  handleTouchStart(event) {
    startY = event.touches[0].clientY;
      this.setData({
       coverTransition:''
     })
  },
  handleTouchMove(event) {
    moveY = event.touches[0].clientY;
    moveDistance = moveY - startY;
    if (moveDistance > 0&& moveDistance< 80) {
      this.setData({
      coverTransform:`translateY(${moveDistance}rpx)`
      })
    }

  },
  handleTouchEnd() {
     this.setData({
       coverTransform: 'translateY(0)',
       coverTransition:'transform .3s linear'
     })
  },


  //跳转登录界面
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})